<?php

/**
 * This is the model class for table "account".
 *
 * The followings are the available columns in table 'account':
 * @property integer $id
 * @property string $name
 * @property string $email
 * @property string $phone
 * @property string $nickname
 * @property string $status
 * @property string $crc
 * @property string $password
 * @property string $created_on
 * @property string $modified_on
 */
class Account extends CActiveRecord
{
	const CRC_SECRET_KEY_ACCOUNT = '0erbXNGIkQBJ4RKQKWa0';

	const STATUS_LOCKED = 'LOCKED';
	const STATUS_INIT = 'INIT';
	const STATUS_VERIFIED = 'VERIFIED';

	public $captcha;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Account the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'account';
	}

	function behaviors()
	{
		return array(
			'ActiveRecordCRCBehavior'=>array(
				'class'=>'ext.common.behaviors.ActiveRecordCRCBehavior',
				'crcSecretKey'=>self::CRC_SECRET_KEY_ACCOUNT,
				'crcField'=>'crc',
				'crcFields'=>array('id','password','email','phone','status', 'nickname'),
				'lockedField'=>array('status'=>self::STATUS_LOCKED),
			),
		);
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, email, phone, nickname, password', 'required'),
			array('name, email, crc, password', 'length', 'max'=>255),
			array('phone, nickname', 'length', 'max'=>32),
			array('email', 'email','message'=>"Email không đúng định dạng"),
			array('email', 'unique','message'=>'Email này đã được sử dụng trên hệ thống!'),
			array('nickname', 'unique','message'=>'Nickname này đã được sử dụng trên hệ thống!'),
			array('captcha', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
			array('status', 'length', 'max'=>8),
			array('created_on, modified_on', 'safe'),
			array('created_on, modified_on', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, email, phone, nickname, status, crc, password, created_on, modified_on', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	public function attributesName()
	{
		return array(
			'id',
			'name',
			'email',
			'phone',
			'nickname',
			'status',
			'crc',
			'captcha',
			'password',
			'created_on',
			'modified_on',
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Tên',
			'email' => 'Email',
			'phone' => 'Điện thoại',
			'nickname' => 'Nickname',
			'status' => 'Trạng thái',
			'crc' => 'Crc',
			'password' => 'Mật khẩu',
			'created_on' => 'Ngày tạo',
			'modified_on' => 'Cập nhật gần nhất',
			'captcha' => 'Mã an toàn',
		);
	}

	/**
	 * Function Before Save để lưu lại thời gian khởi tạo AR
	 *
	 */
	public function beforeSave() {
		if ($this->isNewRecord)
			$this->created_on = new CDbExpression('NOW()');

		return parent::beforeSave();
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('nickname',$this->nickname,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('crc',$this->crc,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('created_on',$this->created_on,true);
		$criteria->compare('modified_on',$this->modified_on,true);
		$criteria->order = 'id desc';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Tạo tài khoản
	 * @return bool
	 */
	function createAccount()
	{
		try{
			$this->password = md5($this->password);
			$this->status = self::STATUS_INIT;
			if(!$this->save()) throw new CException(CJSON::encode($this->getErrors()));
			return true;
		}catch(Exception $e){
			Yii::log($e->getMessage(),CLogger::LEVEL_ERROR,__CLASS__);
			return false;
		}
	}
}